package no.aina.xml;

import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
import java.net.URLConnection;
import java.util.zip.GZIPInputStream;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;

import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;

public class XMLReader {

	private Document doc;

	public XMLReader(String uri) throws XMLReaderException {

		try {

			URL url = new URL(uri);
			URLConnection connection = url.openConnection();

			DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
			DocumentBuilder db = factory.newDocumentBuilder();

			connection.getContentType();
			connection.getContentEncoding();

			InputStream inputStream;
			if ("x-gzip".equals(connection.getContentEncoding()) || "application/x-gzip".equals(connection.getContentType())) {
				//			if (connection.getContentEncoding() != null) {
				inputStream = new GZIPInputStream(connection.getInputStream());
			} else {
				inputStream = connection.getInputStream();
			}

			doc = db.parse(inputStream);

		} catch (ParserConfigurationException e) {
			throw new XMLReaderException(e); 
		} catch (SAXException e) {
			throw new XMLReaderException(e); 
		} catch (IOException e) {
			throw new XMLReaderException(e); 
		}

	}

	public String getNodeValue(Element node, String name) throws XMLElementNotFound {
		Element subnode = (Element) node.getElementsByTagName(name).item(0);
		if (subnode == null) {
			throw new XMLElementNotFound();
		}

		String value = subnode.getChildNodes().item(0).getNodeValue();
		return value;
	}
	
   public String getCData(Element node, String name) throws XMLElementNotFound {
      Element subnode = (Element) node.getElementsByTagName(name).item(0);
      if (subnode == null) {
         throw new XMLElementNotFound();
      }

//      xmlDocument.getElementsByTagName('content')[0].childNodes[1].nodeValue;
      String value = subnode.getChildNodes().item(1).getNodeValue();
      return value;
   }


	public String getAttribute(Element node, String name, String attribute) {
		Element subnode = (Element) node.getElementsByTagName(name).item(0);
		if (subnode.getAttributeNode(attribute) == null && name.equals(node.getNodeName())) {

			NodeList childs = node.getElementsByTagName(name);
			for (int i = 0; i < childs.getLength(); i++) {
				if (childs.item(i).getAttributes().getLength() > 0) {
					String value = ((Element)childs.item(i)).getAttributeNode(attribute).getNodeValue();
					return value;
				}
			}

			return null;
		} 

		String value = subnode.getAttributeNode(attribute).getNodeValue();
		return value;
	}

	private Element getElement(Document doc, String name) {
		return (Element) doc.getElementsByTagName(name).item(0);
	}
	public Element getElement(Element node, String name) {
		//		return getElement(node, name, 0);
		return (Element) node.getElementsByTagName(name).item(0);
	}
	public Element getElement(NodeList node, int i) {
		return (Element) node.item(i);
	}
	public NodeList getElements(Element node, String name) {
		return node.getElementsByTagName(name);
	}

	public Element getElement(String name) {
		return getElement(doc, name);
	}

	public class XMLElementNotFound extends RuntimeException {
	}

}
